import java.sql.Timestamp

import org.joda.time.DateTime
import org.json4s.{DefaultFormats, Formats}
import squirrel.{SqlValue, SqlValueBool, SqlValueNull, SqlValueNumber, SqlValueString}

import scala.language.implicitConversions

package object ignite {

  def sqlValueToObject(value: SqlValue): Object = value match {
    case SqlValueBool(b) => b.asInstanceOf[Object]
    case SqlValueNumber(num) =>
      val v = if (num.isWhole()) num.toLong else num.toDouble
      v.asInstanceOf[Object]
    case SqlValueString(str) => str.asInstanceOf[Object]
    case SqlValueNull        => null
  }

  implicit val formats: Formats = DefaultFormats

  implicit def jodaDateTimeToSqlTimestamp(dt: DateTime): java.sql.Timestamp = {
    new Timestamp(dt.getMillis)
  }

}
